<template>
	<!-- https://ext.dcloud.net.cn/plugin?id=8151 -->
	<view class="container">
		<view class="page-body">
			<view class='wrapper'>
				<view class='toolbar' @tap="formatClick">
					<view :class="formats.bold ? 'ql-active' : ''" class="iconfont icon-zitijiacu" data-name="bold"
						data-desc="加粗">
					</view>
					<view :class="formats.italic ? 'ql-active' : ''" class="iconfont icon-zitixieti" data-name="italic"
						data-desc="斜体">
					</view>
					<view :class="formats.underline ? 'ql-active' : ''" class="iconfont icon-zitixiahuaxian"
						data-name="underline" data-desc="下划线"></view>
					<view :class="formats.strike ? 'ql-active' : ''" class="iconfont icon-zitishanchuxian"
						data-name="strike" data-desc="删除线"></view>
					<view :class="formats.align === 'left' ? 'ql-active' : ''" class="iconfont icon-zuoduiqi"
						data-name="align" data-value="left" data-desc="居左"></view>
					<view :class="formats.align === 'center' ? 'ql-active' : ''" class="iconfont icon-juzhongduiqi"
						data-name="align" data-value="center" data-desc="居中"></view>
					<view :class="formats.align === 'right' ? 'ql-active' : ''" class="iconfont icon-youduiqi"
						data-name="align" data-value="right" data-desc="居右"></view>
					<view :class="formats.align === 'justify' ? 'ql-active' : ''" class="iconfont icon-zuoyouduiqi"
						data-name="align" data-value="justify" data-desc="平铺"></view>
					<view :class="formats.lineHeight ? 'ql-active' : ''" class="iconfont icon-line-height"
						data-name="lineHeight" data-value="2" data-desc="行高"></view>
					<view :class="formats.letterSpacing ? 'ql-active' : ''" class="iconfont icon-Character-Spacing"
						data-name="letterSpacing" data-value="2em" data-desc="间距"></view>
					<view :class="formats.marginTop ? 'ql-active' : ''" class="iconfont icon-722bianjiqi_duanqianju"
						data-name="marginTop" data-value="20px" data-desc="上边距"></view>
					<view :class="formats.previewarginBottom ? 'ql-active' : ''"
						class="iconfont icon-723bianjiqi_duanhouju" data-name="marginBottom" data-value="20px"
						data-desc="下边距"></view>
					<view class="iconfont icon-clearedformat" @tap="removeFormat" data-desc="清除样式"></view>
					<view :class="formats.fontFamily ? 'ql-active' : ''" class="iconfont icon-font"
						data-name="fontFamily" data-value="Pacifico" data-desc="字体"></view>
					<picker mode="selector" :range="fontSizeRange" @change="changeFontSize"
						style="display: inline-block;">
						<view :class="formats.fontSize ? 'ql-active' : ''" class="iconfont icon-fontsize"
							data-name="fontSize" :data-value="formats.fontSize" data-desc="字体大小"></view>
					</picker>
					<view @tap="openFontColor" :style="{'color':fontColor}" class="iconfont icon-text_color"
						data-name="color" :data-value="fontColor" data-desc="字体颜色"></view>
					<t-color-picker ref="colorPicker" @confirm="confirmFontColor"></t-color-picker>
					<view @tap="openBgColor" :style="{'color': bgColor}" class="iconfont icon-fontbgcolor"
						data-name="backgroundColor" :data-value="bgColor" data-desc="字体背景色"></view>
					<t-color-picker ref="colorPicker2" @confirm="confirmBgColor"></t-color-picker>
					<view class="iconfont icon-date" @tap="insertDate" data-desc="日期"></view>
					<view class="iconfont icon--checklist" data-name="list" data-value="check" data-desc="列表"></view>
					<view :class="formats.list === 'ordered' ? 'ql-active' : ''" class="iconfont icon-youxupailie"
						data-name="list" data-value="ordered" data-desc="有序列表"></view>
					<view :class="formats.list === 'bullet' ? 'ql-active' : ''" class="iconfont icon-wuxupailie"
						data-name="list" data-value="bullet" data-desc="无序列表"></view>
					<view class="iconfont icon-undo" @tap="undo" data-desc="撤销"></view>
					<view class="iconfont icon-redo" @tap="redo" data-desc="前进"></view>
					<view class="iconfont icon-outdent" data-name="indent" data-value="-1" data-desc="减少缩进"></view>
					<view class="iconfont icon-indent" data-name="indent" data-value="+1" data-desc="增加缩进"></view>
					<view class="iconfont icon-fengexian" @tap="insertDivider" data-desc="分割线"></view>
					<view class="iconfont icon-charutupian" @tap="insertImage" data-desc="上传图片"></view>
					<view :class="formats.header === 1 ? 'ql-active' : ''" class="iconfont icon-format-header-1"
						data-name="header" :data-value="1" data-desc="标题"></view>
					<view :class="formats.script === 'sub' ? 'ql-active' : ''" class="iconfont icon-zitixiabiao"
						data-name="script" data-value="sub" data-desc="幂"></view>
					<view :class="formats.script === 'super' ? 'ql-active' : ''" class="iconfont icon-zitishangbiao"
						data-name="script" data-value="super" data-desc="幂"></view>
					<view class="iconfont icon-shanchu" @tap="clear" data-desc="清除"></view>
					<view :class="formats.direction === 'rtl' ? 'ql-active' : ''" class="iconfont icon-direction-rtl"
						data-name="direction" data-value="rtl" data-desc="反打"></view>
				</view>
				<editor @input="editText" id="editor" class="ql-container" :placeholder="placeholder" showImgSize
					showImgToolbar showImgResize @statuschange="onStatusChange" :read-only="disabled"
					@ready="onEditorReady">
				</editor>
			</view>
		</view>

	</view>
</template>

<script>
	import tColorPicker from '@/components/t-color-picker/t-color-picker.vue'
	export default {
		components: {
			tColorPicker
		},
		props: {
			contentTxt: {
				type: String,
				default: ''
			},
			placeholder: {
				type: String,
				default: '开始输入...'
			},
			disabled: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				readOnly: false,
				formats: {},
				fontSizeRange: [12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50], //字体大小
				fontColor: '', //颜色 color: {r: 255,g: 0,b: 0,a: 0.6}颜色选择器设置初始颜色，对象，后面值也是
				bgColor: '', //字体背景色
			}
		},
		mounted() {

		},
		methods: {
			openFontColor() { // 打开字体颜色选择器
				if (this.disabled) return;
				this.$refs.colorPicker.open();
			},
			confirmFontColor(e) {
				console.log('颜色选择器返回值：' + e)
				console.log(e);
				this.fontColor = e.hex;
				this.editorCtx.format('color', this.fontColor);
				this.$u.msg(this.fontColor)
			},
			openBgColor() { // 打开字体背景颜色选择器
				if (this.disabled) return;
				this.$refs.colorPicker2.open();
			},
			confirmBgColor(e) {
				console.log('颜色选择器返回值：' + e)
				console.log(e);
				this.bgColor = e.hex;
				this.editorCtx.format('backgroundColor', this.bgColor);
				this.$u.msg(this.bgColor)
			},
			changeFontSize(e) { //改变字体的大小
				if (this.disabled) return;
				const index = e.detail.value;
				const fz = this.fontSizeRange[index] + 'px';
				this.editorCtx.format('fontSize', fz);
				this.$u.msg(fz)
			},
			editText(res) { //富文本内容
				this.$emit('editorChange', res.detail.html)
			},
			readOnlyChange() { //只读
				this.readOnly = !this.readOnly
			},
			onEditorReady() {
				let that = this //解决微信小程序不能使用问题
				that.createSelectorQuery().select('#editor').context((res) => {
					this.editorCtx = res.context
					this.editorCtx.setContents({
						html: this.contentTxt //初始化赋值内容。    
					})

				}).exec()
				// uni.createSelectorQuery().select('#editor').context((res) => {
				// 	this.editorCtx = res.context
				// 	this.editorCtx.setContents({
				// 		html: this.contentTxt //初始化赋值内容。    
				// 	})

				// }).exec()

			},
			undo() {
				if (this.disabled) return;
				this.$u.msg('撤回')
				this.editorCtx.undo()
			},
			redo() {
				if (this.disabled) return;
				this.$u.msg('前进')
				this.editorCtx.redo()
			},
			formatClick(e) {
				if (this.disabled) return;
				let {
					name,
					value,
					desc
				} = e.target.dataset
				if (!name) return
				// console.log('format', name, value)
				this.$u.msg(desc)
				this.editorCtx.format(name, value)
			},
			onStatusChange(e) {
				if (this.disabled) return;
				const formats = e.detail
				this.formats = formats
			},
			insertDivider() {
				if (this.disabled) return;
				this.$u.msg('分割线')
				this.editorCtx.insertDivider({
					success: function() {
						console.log('insert divider success')
					}
				})
			},
			clear() {
				if (this.disabled) return;
				this.$u.msg('清除内容')
				this.editorCtx.clear({
					success: function(res) {
						console.log("clear success")
					}
				})
			},
			removeFormat() {
				if (this.disabled) return;
				this.$u.msg('清除样式')
				this.editorCtx.removeFormat()
				this.fontColor = ''
				this.bgColor = ''
			},
			insertDate() {
				if (this.disabled) return;
				this.$u.msg('插入日期')
				const date = new Date()
				const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
				this.editorCtx.insertText({
					text: formatDate
				})
			},
			insertImage() {
				if (this.disabled) return;
				uni.chooseImage({
					count: 1,
					success: (res) => {
						this.$http.upload('/common/upload', res
							.tempFilePaths[0]).then(res => {
							if (res.data.code == 200) {
								console.log(res.data.data)
								this.editorCtx.insertImage({
									src: res.data.data,
									alt: '图像',
									success: function() {
										console.log('insert image success')
									}
								})
							} else {
								this.$u.msg(res.data.msg)
							}
						})
					}
				})
			}
		},
		onLoad() {
			uni.loadFontFace({
				family: 'Pacifico',
				source: 'url("./Pacifico.ttf")'
			})
		},
	}
</script>

<style>
	@import "./cs.css";

	.wrapper {
		padding: 5px;
	}

	.toolbar {
		border: 1upx #999 solid;
	}

	.iconfont {
		display: inline-block;
		padding: 8px 8px;
		width: 24px;
		height: 24px;
		cursor: pointer;
		font-size: 20px;
	}

	.toolbar {
		box-sizing: border-box;
		border-bottom: 0;
		font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
	}

	.ql-container {
		box-sizing: border-box;
		padding: 12px 15px;
		width: 100%;
		min-height: 30vh;
		height: auto;
		background: #fff;
		font-size: 16px;
		line-height: 1.5;
		border: 1px #999 solid;
	}

	.ql-active {
		color: #06c;
	}
</style>